<?php

require_once("Model/ModelsInclude.php");

require_once("Layout/header.php");
require_once("Layout/controllersPageLayout.php");

require_once("Lib/Utils.php");

define("ERROR_MSG", "Stampa fallita!");

/*
 * Istanza generica per accesso ad attributi del model.
 */
$modelToInstanziate = $_GET["ModelName"];
$modelInstanceId = $_GET["ID"];
if($modelToInstanziate == "" || $modelInstanceId == "" || !class_exists($modelToInstanziate))
{
	echo("<p style=\"color:#f00;\"><b>Parametri della richiesta non validi!</b></p>");
	die();
}
$c = new $modelToInstanziate();
if(!$c->isPrintAvailable())
{
	echo("<p style=\"color:#f00;\"><b>Parametri della richiesta non validi!</b></p>");
	die();
}

/*
 * Connessione DB.
 */
$db = new DatabaseManager();
$db->Connect();

/*
 * Aggiunta a DB.
 */
$newObj = $c->getFromDB($db,$modelInstanceId);

if($newObj != NULL)
{
	?>
<!DOCTYPE html>
<html>
<head>
    <title>Stampa</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body align='center'>
	<div align='center'>
		<div id="mainContainer" style="width: 1000px;">

	<?php
	//si esegue la stampa o si stampa un messaggio di errore
	$res = $newObj->printHTMLFile($db);
	switch($res)
	{
		case StdModel::PRINT_DB_FAILED: echo("<p style=\"color:#f00;\"><b>Errore stampa: Si prega di riprovare.</b><br/><br/>Messaggio di errore: ".$newObj->getLastPrintError()." - Query: ".$db->lastQuery."</p>"); break;//printFailPage(ERROR_MSG, $db->lastQuery); break;
		case StdModel::PRINT_FILE_FAILED: echo("<p style=\"color:#f00;\"><b>Errore stampa: Si prega di riprovare.</b><br/><br/>Messaggio di errore: ".$newObj->getLastPrintError()."</p>"); break;
		case StdModel::PRINT_SUCCESS: break;
	}
	?>

		</div>
	</div>
<?php
if(Utils::getDeviceType() == Utils::CLIENT_DEVICE_DESKTOP)
{
	echo('<script type="text/javascript" src="'.TINY_MCE_INSTALL_DIR.'"></script>');
	?>
	<script type="text/javascript">
	tinymce.init({
		selector: "div#mainContainer",
		plugins: [
			"advlist autolink lists link image charmap print preview anchor",
			"searchreplace visualblocks code fullscreen",
			"insertdatetime media table contextmenu paste"
		],
		width: 1030,
		toolbar: "insertfile undo redo | styleselect formatselect fontselect fontsizeselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
		visual: false
	});
	</script>
	<?php
}
else
{
	?>
	<script type="text/javascript">
	window.onload = function() { setTimeout( function() { window.print(); } , 1000); };
	</script>
	<?php
}
?>
</body>
</html>
<?php
}
else
	printFailPage(ERROR_MSG, $db->lastQuery);

?>
